
import axios from 'axios'
import Setting from '@/setting'
import util from '@/libs/util'
import { updatefileTemp } from '@/api/set.js'
import { Message } from "view-ui-plus";
var baseUrl = Setting.request.apiBaseURL
var requestAction = baseUrl + `/v1/upload/images?auth_access_token=${util.cookies.get('token')}&auth_project_id=${localStorage.getItem('currentProject')}`
var that=this;
class MyUploadAdapter {

    constructor(loader) {

        this.loader = loader

    }

    // 启动上载过程

    upload () {
        
        return this.loader.file

            .then(file => new Promise((resolve, reject) => {
                if(file.size>500*1024){
                    Message.warning("上传失败，图片大小请控制在 500K 以内");
                    reject() 
                   return;
                }
                var formdata = new FormData()

                formdata.append("file", file)
                axios.post(requestAction, formdata).then(res => {
                    updatefileTemp({id:res.data.data['image_id'][0]}).then(res=>{
                        // console.log(res)
                    })
                    resolve({
                        default: res.data.data.host + res.data.data.image_url[0]
                    })
                })
            }))

    }

    // 中止上载过程

    abort () {
        if (this.xhr) {

            this.xhr.abort()

        }

    }

}

function MyCustomUploadAdapterPlugin (editor) {
    editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
        // 在这里将URL配置为后端上载脚本
        return new MyUploadAdapter(loader)

    }

}

export {

    MyUploadAdapter,

    MyCustomUploadAdapterPlugin

}